
import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Administrator
 */
public class Parking {

    //public static Stack<Car>[] slots = new ArrayParkingSlot[10];
    public static Stack<Car>[] slots = new ListParkingSlot[10];
    public static Car[] cars = new Car[110];

    public static void main (String args[]) throws StackOverflow, StackUnderflow{

        Scanner input = new Scanner(System.in);
        int contCar = 0;
        String opcion = "";
        String[] subopcion = new String[3];
        boolean bmenu = true;
        String menu = "Instrucciones:\n"+
                    "Entrar: Ingrese el comando \"A #placa\"\n"+
                    "Salir: Ingrese el comando \"D #placa id\"\n"+
                    "Imprimir Estacionemiento: Ingrese el comando \"P id\"";
        for (int i = 0; i < 10; i++){
            //slots[i] = new ArrayParkingSlot<Car>(10);
            slots[i] = new ListParkingSlot<Car>(10);
        }
           

        for (int i = 0; i < 110; i++)
            cars[i] = new Car();

        do{
            System.out.println(menu);
            try{
                opcion = input.nextLine();
                opcion = opcion.toUpperCase();

                subopcion = opcion.split(" ");

                if (subopcion[0].equals("A")){
                    cars[contCar].setPlaca(subopcion[1]);
                    inputCar(contCar);

                    assert cars[contCar].equals(slots[Integer.parseInt(cars[contCar].getParkinId())].peek()):"Sta malo";
                    System.out.println("Su Id es: "+ cars[contCar].getParkinId());
                    //System.out.println(slots[0].pop().getPlaca());
                    contCar++;
                }

                else

                if (subopcion[0].equals("D")){
                    if (contCar > 0)
                        contCar--;
                    cars[contCar] = outputCar(subopcion[1],subopcion[2]);
                    cars[contCar].setParkingId("");
                    cars[contCar].setPlaca("");
                }

                else

                if (subopcion[0].equals("P")){
                    System.out.println(printSlot(Integer.parseInt(subopcion[1])));
                }

                else
                    System.out.println("Instruccion Incorrecta");
            }
            catch(StackOverflow overflow){
                System.out.println("El parqueo esta lleno!! Necesita retirar un vehiculo");
                System.err.println(overflow);
            }
            catch(StackUnderflow underflow){
                System.out.println("El parqueo esta vacio");
                System.out.println(underflow);
            }

        }while(bmenu);
    }

    public static void inputCar(int i) throws StackOverflow, StackUnderflow{

        cars[i].setParkingId("" + buscarParqueo());
        slots[buscarParqueo()].push(cars[i]);
    }

    public static int buscarParqueo(){

        for (int i = 0; i < 10; i++){
            if (slots[i].getIndex() < 9)
            return(i);
        }
        return(9);
    }

    public static Car outputCar(String p, String id) throws StackOverflow, StackUnderflow{
        Car cartemp = new Car();
        Stack<Car> slottemp = new ArrayParkingSlot(10);
        boolean found = false;
        int i = slots[Integer.parseInt(id)].getIndex();

        while((!found) && (i >= 0)){
            if(p.equals(slots[Integer.parseInt(id)].peek().getPlaca())){
                found = true;
                cartemp = slots[Integer.parseInt(id)].pop();
            }
            else{
                slottemp.push(slots[Integer.parseInt(id)].pop());
                i--;
            }
        }

        if (slottemp.getIndex() >= 0)
            for(i = slottemp.getIndex(); i >= 0; i--)
                slots[Integer.parseInt(id)].push(slottemp.pop());

        return(cartemp);
    }

    public static String printSlot(int index) throws StackOverflow, StackUnderflow{

        Stack<Car> slottemp = new ArrayParkingSlot(10);
        //Stack<Car> slottemp = new ListParkingSlot(10);

        String s = "";

        s = s + ("________");
        s = s + ("\n_____ "+index+"\n");
        for (int i = 9; i >= 0; i--){
            s = s + ("     |");
            if (i <= slots[index].getIndex()){
                slottemp.push(slots[index].peek());
                System.out.println("Este peek o push no es el error");
                s = s + (slots[index].pop().getPlaca());
            }

            else
                s = s + ("   ");
            s = s + ("|\n");
        }

        if (slottemp.getIndex() >= 0){
            System.out.println("El tamaño del temporal: "+ slottemp.getIndex());
            for(int i = slottemp.getIndex(); i >= 0; i--)
                slots[index].push(slottemp.pop());
        }
            

        return(s);
    }
}